<template>
    <body>
        <div class="container">
            <div class="form-box" ref="formBox">
                <!-- 登录模块 -->
                <div class="login-box">
                    <h1>登录</h1>
                    <input v-model="username" id="username" type="text" placeholder="用户名">
                    <input v-model="password" type="password" placeholder="密码">
                    <button id="loginButton" @click="login">登录</button>
                </div>
            </div>
            <div class="con-box right">
                <h2>欢迎管理员登录</h2>
                <p>请告诉我你的账号密码～</p>
            </div>
        </div>
    </body>
</template>
  
<script>
import $ from 'jquery'

import router from '@/router';
export default {
    name: "LoginView",
    components: {

    },
    data() {
        return {
            username: '',
            password: '',
        }
    },
    methods: {
        login() {
            let username = this.username;
            let str1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-/=!@#$%^&*';
            let str2 = 'L73hz/sHgnKmQCD8YFfGj1toaR0S4JcTpi6rv5yw+BVbWPeNu2IlUOxEdqkM9XZ+A-*&^%$#@!';
            let encoded = '';
            for (let i = 0; i < this.password.length; i++) { //简单的可逆加密，为了防止post时浏览器认为密码泄露，后端还会使用hash方法进行严格的加密
                let k = str1.indexOf(this.password[i]);
                if (k == -1) {
                    alert("密码只能包含大小写英文字母、数字和'!@#$%^&*+-/='这些特殊字符");
                    return;
                }
                encoded += str2[k];
            }
            if (this.username === '' || this.password === '') {
                alert('账号名和密码不能为空！');
            }
            else {
                $.ajax({
                    url: "http://10.142.225.1:3000/user/account/token",
                    type: "post",
                    data: {
                        username: this.username,
                        password: encoded
                    },
                    success(resp) {
                        if (resp.message === "Success!" && resp.purview === 'Super Admin') {
                            localStorage.setItem("is_login", true);
                            localStorage.setItem("CurUser", username);
                            localStorage.setItem("token", resp.token);
                            router.push({ name: "manage" });
                        }
                        else alert("非管理员账户");
                    },
                    error() {
                        alert("账号名或密码错误!");
                    }
                })
            }
        }
    },
};

</script>
  
<style scoped>
* {
    /* 初始化 */
    margin: 0;
    padding: 0;
}

body {
    /* 100%窗口高度 */
    height: 100vh;
    /* 水平+垂直居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    /* 设置背景 */
    /*纯色背景*/
    /*background: linear-gradient(200deg, #e7f3f3, #e3eeff);*/
    /*图片背景*/
    background-image: url(../assets/背景3.png);
    background-size: cover;
    /* 将背景图片按比例缩放，以覆盖整个背景区域 */
    background-repeat: no-repeat;
    /* 禁止背景图片重复 */
    background-position: center center;
    /* 将背景图片水平垂直居中 */
}

.container {
    background-color: lightcyan;
    width: 650px;
    height: 415px;
    border-radius: 5px;
    /* 阴影 */
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
    /* 相对定位 */
    position: relative;
}

/*主体框*/
.form-box {
    /* 绝对定位 */
    position: absolute;
    top: -10%;
    left: 5%;
    background-color: #46c1d3;
    width: 320px;
    height: 500px;
    border-radius: 5px;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2;
    /* 动画过渡 加速后减速 */
    transition: 0.5s ease-in-out;
}

.register-box,
.login-box {
    /* 弹性布局 垂直排列 */
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.hidden {
    display: none;
    transition: 0.5s;
}

/*标题*/
h1 {
    text-align: center;
    margin-bottom: 25px;
    /* 大写 */
    text-transform: uppercase;
    color: #fff;
    /* 字间距 */
    letter-spacing: 5px;
}

input {
    background-color: transparent;
    width: 70%;
    color: #fff;
    border: none;
    /* 下边框样式 */
    border-bottom: 1px solid rgba(255, 255, 255, 0.4);
    padding: 10px 0;
    text-indent: 10px;
    margin: 8px 0;
    font-size: 14px;
    letter-spacing: 2px;
}

/*输入框*/
input::placeholder {
    color: #fff;
}

input:focus {
    color: #0a47b0;
    outline: none;
    border-bottom: 1px solid #1255c880;
    transition: 0.5s;
}

input:focus::placeholder {
    opacity: 0;
}

/* 登录、注册按钮格式 */
.form-box button {
    width: 70%;
    margin-top: 35px;
    background-color: #f6f6f6;
    outline: none;
    border-radius: 8px;
    padding: 13px;
    color: #2460c0;
    letter-spacing: 2px;
    border: none;
    cursor: pointer;
}

.form-box button:hover {
    background-color: #2460c0;
    color: #f6f6f6;
    transition: background-color 0.5s ease;
}

.con-box {
    width: 50%;
    /* 弹性布局 垂直排列 居中 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    /* 绝对定位 居中 */
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

.con-box.left {
    left: -2%;
}

.con-box.right {
    right: -2%;
}

/*图片上方小标题格式*/
.con-box h2 {
    color: #4183cf;
    font-size: 25px;
    font-weight: bold;
    letter-spacing: 3px;
    text-align: center;
    margin-bottom: 4px;
}

/*图片上方文字格式*/
.con-box p {
    font-size: 12px;
    letter-spacing: 2px;
    color: #689af0;
    text-align: center;
}

/* 此处为照片格式 */
.con-box img {
    width: 200px;
    height: 200px;
    opacity: 0.9;
    margin: 20px 0;
    border-radius: 8px;
    /*圆角*/
}

/* 此处为去登陆、注册按键格式 */
.con-box button {
    margin-top: 3%;
    background-color: #fff;
    color: #3184cc;
    border: 1px solid #50b3ec;
    padding: 6px 10px;
    border-radius: 5px;
    letter-spacing: 1px;
    outline: none;
    cursor: pointer;
}

.con-box button:hover {
    background-color: #50b3ec;
    color: #fff;
    transition: background-color 0.5s ease;
}
</style>